# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("@ot_hack_bazelbuild_buildtools//buildifier:def.bzl", "buildifier", "buildifier_test")
load("@lowrisc_lint//rules:rules.bzl", "licence_test")
load("//rules:quality.bzl", "clang_format_check", "clang_format_test", "html_coverage_report")
load("@rules_rust//rust:defs.bzl", "rustfmt_test")

package(default_visibility = ["//visibility:public"])

licence_test(
    name = "license_check",
    exclude_patterns = [
        # Exclude anything in vendored directories
        "third_party/**",
        "*/vendor/*/*",
        "util/lowrisc_misc-linters/*",

        ## Hardware Exclusions

        # DV Vendored test vectors
        "hw/dv/sv/test_vectors",
        # Vendored CRC model
        "hw/dv/dpi/usbdpi/usb_crc.c",
        # Vendored HMAC model
        "hw/ip/hmac/dv/cryptoc_dpi/*",
        # PRINCE C++ reference model from Sebastien Riou
        "hw/ip/prim/dv/prim_prince/crypto_dpi_prince/prince_ref.h",
        # PRESENT C reference model from Thomas Siebert
        "hw/ip/prim/dv/prim_present/crypto_dpi_present/boxes.inc",
        "hw/ip/prim/dv/prim_present/crypto_dpi_present/comline.inc",
        "hw/ip/prim/dv/prim_present/crypto_dpi_present/present.inc",
        "hw/ip/prim/dv/prim_present/crypto_dpi_present/verbose.inc",
        # PRESENT Python reference model
        "util/design/lib/Present.py",
        # Vendored FPGA information
        "hw/top_earlgrey/data/*.xdc",
        "util/fpga/bram_load.mmi",
        # Manually checked, files contain additional copyright header lines
        "hw/ip/usb_fs_nb_pe/rtl/*",
        "hw/ip/usbdev/rtl/usbdev_iomux.sv",

        ## Software Exclusions

        # Coremark Vendored Files
        "sw/device/benchmarks/coremark/top_earlgrey/*",

        ## Other Exclusions

        # Other Licences,
        "util/wavegen/LICENSE.wavedrom",
        # Site Assets
        "site/**/assets/scss/**",
        "site/landing/static/js/tiny-slider.js",
        # Code taken from Chromium, so covered by the BSD licence
        "sw/otbn/crypto/modexp.s",
        "sw/otbn/crypto/p256.s",
        "sw/otbn/crypto/rsa_verify.s",
        "sw/otbn/crypto/handwritten/rsa_verify_3072.s",
        "sw/otbn/crypto/sha512.s",
        # Mersenne Twister PRNG
        "sw/device/sca/lib/prng.c",
        # Alma testbench files
        "hw/ip/aes/pre_sca/alma/cpp/testbench.h",
        "hw/ip/aes/pre_sca/alma/cpp/verilator_tb_aes_sbox.cpp",
        "hw/ip/aes/pre_sca/alma/cpp/verilator_tb_aes_sub_bytes.cpp",
        "hw/ip/kmac/pre_sca/alma/cpp/verilator_tb_keccak_2share.cpp",
    ],
    licence = """
    Copyright lowRISC contributors.
    Licensed under the Apache License, Version 2.0, see LICENSE for details.
    SPDX-License-Identifier: Apache-2.0
    """,
    workspace = "//:WORKSPACE",
)

clang_format_exclude = [
    # Vendored source code dirs
    "./**/vendor/**",
    # Rust cargo build dirs
    "./**/target/**",
    # Directories used exclusively to store build artifacts are still copied into.
    "./build-out/**",
    "./build-bin/**",
    # fusesoc build dir
    "./build/**",
]

clang_format_test(
    name = "clang_format_check",
    exclude_patterns = clang_format_exclude,
    mode = "diff",
    workspace = "//:WORKSPACE",
)

clang_format_check(
    name = "clang_format_fix",
    exclude_patterns = clang_format_exclude,
    mode = "fix",
)

buildifier_exclude = [
    "./WORKSPACE",  # Prevent Buildifier from inserting unnecessary newlines.
    "./**/vendor/**",
    "./build/**",
    "./third_party/rust/crates/crates.bzl",  # Autogenerated by cargo-raze
]

buildifier(
    name = "buildifier_fix",
    exclude_patterns = buildifier_exclude,
)

buildifier_test(
    name = "buildifier_check",
    diff_command = "diff -u",
    exclude_patterns = buildifier_exclude,
    mode = "diff",
    no_sandbox = True,
    verbose = True,
    workspace = "//:WORKSPACE",
)

# TODO(cfrantz): Find a way to keep this list of rust targets synchronized
# with the full set of rust targets in the codebase.
#
# $ bazel query 'kind("rust_(binary|library|test)", //...)' | grep -v bindgen
RUST_TARGETS = [
    "//sw/device/lib/ujson/rust:roundtrip_test",
    "//sw/host/cryptotest:cryptotest_parser",
    "//sw/host/cryptotest:cryptotest_parser_test",
    "//sw/host/opentitanlib:opentitanlib",
    "//sw/host/opentitanlib:opentitanlib_test",
    "//sw/host/opentitansession:opentitansession",
    "//sw/host/opentitantool:opentitantool",
    "//sw/host/tests/chip/gpio:gpio",
    "//sw/host/tests/chip/power_virus:power_virus",
    "//sw/host/tests/rom/e2e_bootstrap_disabled:e2e_bootstrap_disabled",
    "//sw/host/tests/rom/e2e_bootstrap_entry:e2e_bootstrap_entry",
    "//sw/host/tests/rom/e2e_chip_specific_startup:e2e_chip_specific_startup",
    "//sw/host/tests/rom/sw_strap_value:sw_strap_value",
    "//sw/host/tests/chip/spi_passthru:spi_passthru",
]

rustfmt_test(
    name = "rustfmt_check",
    targets = RUST_TARGETS,
)

html_coverage_report(
    name = "html_coverage_report",
)
